package com.xsmart.goods.services;

import com.baomidou.mybatisplus.plugins.Page;
import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.goods.dao.GoodsTypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author stom
 * @version V1.0.0
 */

@Service
public class GoodsTypeService {
    @Autowired
    GoodsTypeDao goodsTypeDao;
    /**
     * 查询商品类型
     *
     * @param requestBean 请求对象
     * @return 返回对象
     */
    public ResponseBean queryGoodsType(RequestBean requestBean) {
        int currentPage = requestBean.getRequestParamInteger("currentPage", 1);
        int pageSize = requestBean.getRequestParamInteger("pageSize", 10);
        String cat_name = requestBean.getRequestParamString("cat_name");
        Integer enabled = requestBean.getRequestParamInteger("enabled");
        Map<String, Object> goodsTypeMap = new HashMap<>();
        goodsTypeMap.put("cat_name",cat_name);
        goodsTypeMap.put("enabled",enabled);
        ResponseBean responseBean = requestBean.getResponseBean();
        Page<Map<String, Object>> page = new Page<>(currentPage, pageSize);
        page.setRecords(goodsTypeDao.queryGoodsType(page,goodsTypeMap));
        responseBean.addResultPageItem("goodsTypeList", page);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 新增商品类型
     * @param requestBean
     * @return
     */
    public ResponseBean addGoodsType(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        String cat_name = requestBean.getRequestParamString("cat_name");
        Integer enabled = requestBean.getRequestParamInteger("enabled");
        Integer user_id = requestBean.getRequestParamInteger("user_id");
        String attrGroup=requestBean.getRequestParamString("attr_group");
        Map<String, Object> goodsTypeMap = new HashMap<>();
        goodsTypeMap.put("cat_name",cat_name);
        goodsTypeMap.put("enabled",enabled);
        goodsTypeMap.put("user_id",user_id);
        goodsTypeMap.put("attr_group",attrGroup);
        int num = goodsTypeDao.addGoodsType(goodsTypeMap);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 修改商品类型
     * @param requestBean
     * @return
     */
    public ResponseBean modGoodsType(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        String cat_name = requestBean.getRequestParamString("cat_name");
        Integer enabled = requestBean.getRequestParamInteger("enabled");
        Integer user_id = requestBean.getRequestParamInteger("user_id");
        Integer cat_id = requestBean.getRequestParamInteger("cat_id");
        String attrGroup=requestBean.getRequestParamString("attr_group");
        Map<String, Object> goodsTypeMap = new HashMap<>();
        goodsTypeMap.put("cat_name",cat_name);
        goodsTypeMap.put("enabled",enabled);
        goodsTypeMap.put("cat_id",cat_id);
        goodsTypeMap.put("user_id",user_id);
        goodsTypeMap.put("attr_group",attrGroup);
        int num = goodsTypeDao.modGoodsType(goodsTypeMap);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 删除商品类型
     * @param requestBean
     * @return
     */
    public ResponseBean delGoodsType(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer cat_id = requestBean.getRequestParamInteger("cat_id");
        int num = goodsTypeDao.delGoodsType(cat_id);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 查询所有的商品类型
     * @param requestBean
     * @return
     */
    public ResponseBean queryAllGoodsType(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        String attrGroup=requestBean.getRequestParamString("attr_group");
        List<Map<String,Object>> goodsTypeList = goodsTypeDao.queryAllGoodsType(attrGroup);
        responseBean.addResultItem("goodsTypeList",goodsTypeList);
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }
}
